<?php

namespace app\index\controller;


use app\admin\model\Category;
use app\index\model\Goods;
use app\index\model\Member;
use app\index\model\Orders;
use think\facade\Request;
use think\facade\Session;

class Index extends Base
{
    public function index()
    {
        $catelist=Category::where('pid','=','0')->order('sort asc')->select();
        $this->assign('catelist',$catelist);
        $list = Goods::order('goods_id desc')->paginate(60);
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function my(){
        if (Session::get('mem_name') == '') {
            $this->error('请先登录', 'memLogin');
        }
        echo "开发中";
    }
    public function goodsDetail(){
        $goods=Goods::find(Request::param('id'));
        $piclist=explode(',',$goods['pics']);
        $piclist=array_filter($piclist,function ($value){
            return !empty($value);
        });
        $this->assign('piclist',$piclist);
        $this->assign('goods',$goods);
        return $this->fetch();
    }

    public function memLogin()
    {
        return $this->fetch();
    }

    public function memLoginChk()
    {
        $data = Request::param();
        $query = Member::where('mem_name', $data['mem_name'])->where('pass', md5(md5($data['pass'])))->select();
        if (count($query) == 1) {
            Session::set('mem_name', $data['mem_name']);
            Session::set('nick_name', $query[0]['nick_name']);
            Session::set('m_id', $query[0]['m_id']);
            $this->redirect('index');
        } else {
            $this->error("账号或密码错误", 'memLogin');
        }
    }

    public function memQuit()
    {
        Session::clear();
        $this->success('退出登录', 'index');
    }

    public function memReg()
    {
        return $this->fetch();
    }

    public function memRegSave()
    {
        $data = Request::param();
        if ($data['pass'] != $data['pass2']) {
            $this->error("两次密码输入不一致");
        } else {
            $data['pass'] = md5(md5($data['pass']));
            unset($data['pass2']);
        }
        $data['nick_name'] = time();
        $secMem = Member::where('mem_name', $data['mem_name'])->select();
        if (count($secMem) >= 1) {
            $this->error("会员名已存在");
        }

        $mem = new Member();
        if ($mem->save($data)) {
            $this->success("注册会员成功", 'index');
        } else {
            $this->error("注册会员失败", 'index');
        }
    }

    function buyGoods()
    {
        if (Session::get('mem_name') == '') {
            $this->error('请先登录', 'memLogin');
        }
        $mem = Member::find(Session::get('m_id'));
        $goods = Goods::find(Request::param('id'));
        $data['mem_id'] = $mem->m_id;
        $data['goods_id'] = $goods->goods_id;
        $data['goods_title'] = $goods->goods_title;
        $data['total_num'] = Request::param('buynum');
        $data['total_price'] = $goods->goods_price*Request::param('buynum');
        $data['o_id']=date('YmdHis',time()).rand(1000,1999).$data['mem_id'];
        $data['opic']='';
        $data['price']=Request::param('price');
        $data['nick_name']=$mem->nick_name;

        foreach (explode(',',$goods->pics) as $p){
            if($p!=''){
                $data['opic']=$p;
                break;
            }
        }

        if($goods['stock']>0){
            $goods_data['stock']=$goods['stock']-1;
        }else{
            $this->error('库存不足','goodsDetail/id/'.Request::param('id'));
        }

        $order = new Orders();
        if ($order->save($data)) {
            $goods_data['sale_actual']=$goods['sale_actual']+1;

            $goods->where('goods_id',Request::param('id'))->update($goods_data);
            $this->redirect('orders_list');
        } else {
            $this->error('下单失败', 'index');
        }
    }

    /**
     * 会员订单列表
     * @return void
     */
    public function orders_list()
    {
        $list = Orders::order('order_id', 'desc')->paginate(20);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 分类
     * @return void
     */
    public function classify(){
        $id=Request::param('id');
        $this->assign('id',$id);
        if($id!=''){
            $list = Goods::where('cid','eq',$id)->order('goods_id desc')->paginate(5);
        }else{
            $list = Goods::order('goods_id desc')->paginate(5);
        }

        $this->assign('list', $list);
        $cate=Category::select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }

    public function cateDetail(){
        $id=Request::param('id');
        $this->assign('id',$id);
        if($id!=''){
            $list = Goods::where('cid','eq',$id)->order('goods_id desc')->paginate(60);
        }else{
            $list = Goods::order('goods_id desc')->paginate(60);
        }

        $this->assign('list', $list);
        $cate=Category::select();
        $this->assign('cate',$cate);
        $this->assign('name', Request::param('name'));
        return $this->fetch();
    }
}